//活动
exports.participationActivity = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    //任务流创建
    async.waterfall([

            //1.活动信息查询
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_forum_activity + "/fa_id/" + restfulParams.fa_id,
                        method: 'POST',
                        data: {
                            fields: ["fa_id","fa_rule_text","fa_title", "fa_content","fa_zan_count", "fa_end_time", "fa_collect_count", "fa_comment_count","fa_price", "fa_read_score", "fa_share_score", "fa_join_score", "fa_browse_count", "fa_share_count", "fa_join_count", "fa_create_time"].join(",")
                        }
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.forumActivityData = data.data;
                        cb();
                    }
                );

            },
            //2.评论与回复
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_forum_activity_comment + "/fac_faid/"  + restfulParams.fa_id + "/fac_fid/0",
                        method: 'POST',
                        data  : {
                            fields : [
                                "fac_id as fcc_id",
                                "fac_faid as fcc_faid",
                                "fac_uid as fcc_uid",
                                "fac_content as fcc_content",
                                "fac_reply_uid as fcc_reply_uid",
                                "fac_is_reply as fcc_is_reply",
                                "fac_zan_count as fcc_zan_count",
                                "fac_is_author_commnet as fcc_is_author_commnet",
                                "fac_create_time as fcc_create_time",
                                "fac_fid as fcc_fid",
                                "u_nickname",
                                "u_header_url"
                            ].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.activityCommentData = data.data;
                        cb();
                    }
                );

            },
            //查询当前用户是否对文章评论点赞
            function (cb) {

                //创建添加任务
                var tasks = [];

                //循环节点列表
                resultData.activityCommentData.forEach(function (item, i) {

                    //创建每一个插入任务
                    tasks.push(function (cb) {

                        //查询对该用户的回复
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.searchActivityCommentZanRecord + "/is_delete/0/faczr_fid/" + item.fcc_id + "/faczr_uid/" + restfulParams.u_id,
                                method: 'POST',
                                data: {
                                    fields: ["faczr_id"].join(",")
                                }
                            },
                            function (err, data) {

                                var is_exist = data.data;
// //console.log(is_exist);
                                if (is_exist.length <= 0) {
                                    item.isZan = 0;
                                } else {
                                    item.isZan = 1;
                                }
                                cb();
                            });
                    });
                });
                async.waterfall(tasks, cb);

            },
             //2.评论与回复2
             function (cb) {
                
                //创建添加任务
            var tasks = [];

            //循环节点列表
            resultData.activityCommentData.forEach(function (item, i) {

                //创建每一个插入任务
                tasks.push(function (cb) {

                    //console.log(item);

                    //查询对该用户的回复
                    R.SEND_HTTP(
                        req,
                        {
                            url    : CORE_APIConfig.coreAPI.search_forum_activity_comment+"/fac_fid/"+item.fcc_id+"/count/1",
                            method : 'POST',
                            data:{
                                fields:[
                                    "fac_id as fcc_id",
                                    "fac_content as fcc_content",
                                    "u_nickname",
                                    ].join(",")
                            }
                        },
                        function (err, data) {

                            item.replyCount  = data.count;
                            item.replyData  = data.data;
        
                            cb();
                        });

                });

            });

            async.waterfall(tasks, cb);

            },

            //3.参与者信息
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_forum_activity_join_user+"/fauj_faid/"+restfulParams.fa_id+"/skip/0/limit/10",
                        method: 'POST',
                        data: {
                            fields: ["u_nickname", "u_header_url","fauj_integral"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.intergralData = data.data;
                        cb();
                    }
                );
               
            },
        //查询当前用户是否已收藏该活动
        function (cb) {
            if (restfulParams.u_id) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.userSearchCollect + "/is_delete/0/uc_type/3/uc_fid/" + restfulParams.fa_id + "/uc_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ['uc_id'].join(",")
                        }

                    },
                    function (err, data) {

                        var is_zan = data.data;

                        if (is_zan.length <= 0) {
                            resultData.isCollectData = 0;
                        } else {
                            resultData.isCollectData = 1;
                        }
                        cb();
                    }
                );
            } else {
                cb();
            }

        },
        //查询当前用户是否点过赞
        function (cb) {
            if (restfulParams.u_id) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchActivityZanRecord + "/is_delete/0/fazr_faid/" + restfulParams.fa_id + "/fazr_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ['fazr_id'].join(",")
                        }

                    },
                    function (err, data) {

                        var is_zan = data.data;

                        if (is_zan <= 0) {
                            resultData.isZanData = 0;
                        } else {
                            resultData.isZanData = 1;
                        }
                        cb();
                    }
                );
            } else {
                cb();
            }

        },
          

        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}